var pageCount = 0;
var pageTag;

// 重置
function reset(){
    pageCount = 0;
    var table = document.body.querySelector('#list-view');
    table.innerHTML='';
    mui('#pullrefresh').pullRefresh().refresh(true);
    pullupRefresh()
}

mui.ready(function () {
    // 切换
    mui("#segmentedControl").on("tap","a",function () {
        var tagIndex = this.getAttribute('tag'); // day || month
        // console.log('tabs',tabs)
        pageTag = tagIndex;
        // 重置
        reset()
    })

})


mui.init({
    pullRefresh: {
        container: '#pullrefresh',
        down: {
            style:'circle',
            callback: pulldownRefresh
        },
        up: {
            auto:true,
            contentrefresh: '正在加载...',
            callback: pullupRefresh
        }
    }
});


function pullupRefresh() {
    // return
    setTimeout(function() {
        mui('#pullrefresh').pullRefresh().endPullupToRefresh((++pageCount > 2)); //参数为true代表没有更多数据了。
        var table = document.body.querySelector('#list-view');
        var cells = document.body.querySelectorAll('#list-view .mui-table-view-cell');
        var newCount = cells.length>0?5:20;//首次加载20条，满屏
        for (var i = cells.length, len = i + newCount; i < len; i++) {
            var li = document.createElement('li');
            li.innerHTML =`
              <div class="mui-rows">
                    <div class="mui-row">
                      <div class="mui-col-xs-9">
                          <div class="tit">直营日结</div>
                          <div class="date">2019-01-12 12:12:12</div>
                      </div>
                      <div class="mui-col-xs-3">
                          <div class="total mui-text-right">0.98</div>
                      </div>
                  </div>
              </div>`;
            table.appendChild(li);
        }
        bindClick(li);
    }, 500);
}

function addData() {
    var table = document.body.querySelector('#list-view');
    var cells = document.body.querySelectorAll('#list-view .mui-table-view-cell');
    for(var i = cells.length, len = i + 5; i < len; i++) {
        var li = document.createElement('li');
        li.className = 'mui-table-view-cell';
        li.innerHTML = '<a class="mui-navigate-right">Item ' + (i + 1) + '</a>';
        //下拉刷新，新纪录插到最前面；
        table.insertBefore(li, table.firstChild);
        bindClick(li);
    }
}
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
    setTimeout(function() {
        addData();
        mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
        mui.toast("为你推荐了5篇文章");
    }, 1000);
}

function bindClick(ele) {

    mui("#list-view").off("tap","li");
    mui("#list-view").on('tap',"li",function () {
        // console.log(this.getAttribute('tag'))
        var tag = this.getAttribute('tag'); // 4个判断标识
        console.log('tag',tag);
        // detail
        if(tag){
            // 返回按钮
            mui("#backBtn")[0].style.display = 'none';

            // mui("#pullrefresh1")[0].style.display = 'none';
            mui("#pullrefresh")[0].style.display = 'none';
            mui("#segmentedControlbox")[0].style.display = 'none';
            // mui("#detail")[0].style.display = 'block';
        }
    })
}